import request from "@/utils/request";

// 获取训练状态列表
export function getTrainingStatus() {
  return request({
    url: "/training/bentoml/task_status_options",
    method: "get",
  });
}

// 列出所有训练任务
export function listTrainingTasks(params = {}) {
  return request({
    url: "/training/bentoml/tasks",
    method: "get",
    params: {
      status: params.status || null,
      limit: params.limit || 100,
    },
  });
}

// 搜索训练任务
export function searchTrainingTasks(query, limit = 100) {
  return request({
    url: "/training/bentoml/tasks/search",
    method: "post",
    params: { limit },
    data: {
      task_id: query.task_id || null,
      task_name: query.task_name || null,
      task_status: query.task_status || null,
    },
  });
}

// 获取指定训练任务详情
export function getTrainingTask(taskId) {
  return request({
    url: "/training/bentoml/task/" + taskId,
    method: "get",
  });
}

// 创建训练任务
export function createTrainingTask(data) {
  return request({
    url: "/training/bentoml/create_task",
    method: "post",
    data,
  });
}

// 执行训练任务
export function executeTrainingTask(taskId) {
  return request({
    url: "/training/bentoml/train_task/" + taskId,
    method: "post",
  });
}

// 取消训练任务
export function cancelTrainingTask(taskId) {
  return request({
    url: "/training/bentoml/cancel_task/" + taskId,
    method: "post",
  });
}

// 重试训练任务
export function retryTrainingTask(taskId) {
  return request({
    url: "/training/bentoml/retry_task/" + taskId,
    method: "post",
  });
}

// 获取算法列表
export function getAlgorithmList() {
  return request({
    url: "/algorithms/list",
    method: "get",
  });
}

// 删除指定训练任务
export function delTrainingTask(taskId) {
  return request({
    url: "/training/bentoml/task/" + taskId,
    method: "delete",
  });
}

// 批量删除训练任务（BentoML 接口不支持批量删除，需要循环调用）
export function delTrainingTaskBatch(taskIds) {
  // 由于 BentoML 接口不支持批量删除，这里返回一个 Promise.all
  return Promise.all(
    taskIds.map((taskId) =>
      request({
        url: "/training/bentoml/task/" + taskId,
        method: "delete",
      })
    )
  );
}

// 下载模型文件（使用 model_tag）
export function downloadTrainingModel(modelTag) {
  return request({
    url: "/training/bentoml/download/" + modelTag,
    method: "get",
    responseType: "blob",
  });
}
